<template>
  <div class="app-container">
    <h3>&nbsp;&nbsp;添加讲师</h3>
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="teacherForm.name"/>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacherForm.sort" controls-position="right" :min="0"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacherForm.level" clearable placeholder="请选择">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacherForm.intro"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacherForm.career" :rows="10" type="textarea"/>
      </el-form-item>

      <!-- 讲师头像：TODO -->
      <!-- 讲师头像 -->
      <el-form-item label="讲师头像">

        <!-- 头衔缩略图 -->
        <pan-thumb :image="teacherForm.avatar"/>
        <!-- 文件上传按钮 -->
        <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
        </el-button>

        <!--
    v-show：是否显示上传组件
    :key：类似于id，如果一个页面多个图片上传控件，可以做区分
    :url：后台上传的url地址
    @close：关闭上传组件
    @crop-upload-success：上传成功后的回调
      <input type="file" name="file"/>
    -->
        <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API+'/oss/file/upload'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import teacher from '@/api/teach/teacher'
  import ImageCropper from '@/components/ImageCropper'
  import PanThumb from '@/components/PanThumb'

  const defaultForm = {
    id: '',//讲师id
    name: '',//讲师姓名
    sort: 0,//讲师排序
    level: 1,//讲师头衔
    career: '',//讲师资历
    intro: '',//讲师简介
    avatar: process.env.OSS_PATH + '/avatar/1.jpg',//讲师头像
  };
  export default {
    name: "form",
    components: {ImageCropper, PanThumb},
    data() {
      return {
        teacherForm: defaultForm,//讲师对象
        saveBtnDisabled: false,//保存按钮是否禁用
        buttonInformation: '保存',//按钮信息
        imagecropperShow: false,//上传头像弹框是否显示
        imagecropperKey: 0,//上传头像key值
        BASE_API: process.env.BASE_API,//后端接口地址

      }
    },
    created() {
      //初始化页面
      this.init()
      //
    },
    watch: {//监听路由
      $route(to, from) {//路由发生变化，方法就会执行
        this.init()
      }
    },
    methods: {
      //初始化页面的方法
      init() {
        //判断路径是否有id，如果有，获取id
        if (this.$route.params && this.$route.params.id) {
          this.teacherForm.id = this.$route.params.id
          this.getTeacherById()
        } else {
          //清空表单
          this.teacherForm = {...defaultForm}
        }
      },

      //保存的方法
      saveOrUpdate() {
        //判断是添加还是修改，根据teacherForm对象中是否有id值进行判断
        if (this.teacherForm.id) {
          //修改
          this.update()
        } else {
          //添加
          this.save()
        }
      },

      //修改
      update() {
        teacher.updateTeacher(this.teacherForm)
          .then(response => {//修改成功
            //提示
            this.$message({
              type: 'success',
              message: response.message
            });
            //跳转到讲师列表页面-路由跳转
            this.$router.push({path: '/teach/teacher/list'})
          })
          .catch(response => {//修改失败
            //提示
            this.$message({
              type: 'error',
              message: response.message
            });
          })
      },

      //添加的方法
      save() {
        //禁用保存按钮
        this.saveBtnDisabled = true
        console.log(this.teacherForm)
        teacher.saveTeacher(this.teacherForm)
          .then(response => {//添加成功
            //提示
            this.$message({
              type: 'success',
              message: response.message
            });
            //跳转到讲师列表页面-路由跳转
            this.$router.push({path: '/teach/teacher/list'})
          })
          .catch(response => {//添加失败
            //提示
            this.$message({
              type: 'error',
              message: response.message
            });
          })
      },

      //根据讲师id查询讲师信息
      getTeacherById() {
        teacher.getById(this.teacherForm.id)
          .then(response => {//查询成功
            this.teacherForm = response.data.teacher
          })
      },
      //上传文件成功调用的方法
      cropSuccess(data) {
        //关闭弹框
        this.imagecropperShow = false
        //获取文件URL地址
        this.teacherForm.avatar = data.url
        // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1
      },

      //关闭上传框的方法
      close() {
        //关闭弹框
        this.imagecropperShow = false
        // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1
      }
    }
  }
</script>

<style scoped>

</style>
